package org.example.contorller;

import org.example.pojo.Brand;
import org.example.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 查询所有品牌
     */
    @GetMapping("/queryAll")
    public ResponseEntity<List<Brand>> queryAllBrand() {
        List<Brand> brands = brandService.queryAllBrand();
        return ResponseEntity.ok(brands);
    }

    /**
     * 根据ID查询品牌
     */
    @GetMapping("/{id}")
    public ResponseEntity<Brand> queryBrandById(@PathVariable Integer id) {
        Brand brand = brandService.queryBrandById(id);
        if (brand != null) {
            return ResponseEntity.ok(brand);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 添加品牌
     */
    @PostMapping
    public ResponseEntity<String> addBrand(@RequestBody Brand brand) {
        int result = brandService.addBrand(brand);
        if (result > 0) {
            return ResponseEntity.ok("品牌添加成功");
        } else {
            return ResponseEntity.badRequest().body("品牌添加失败");
        }
    }

    /**
     * 更新品牌
     */
    @PutMapping("/{id}")
    public ResponseEntity<String> updateBrand(@PathVariable Integer id, @RequestBody Brand brand) {
        brand.setId(id);
        int result = brandService.updateBrand(brand);
        if (result > 0) {
            return ResponseEntity.ok("品牌更新成功");
        } else {
            return ResponseEntity.badRequest().body("品牌更新失败");
        }
    }

    /**
     * 删除品牌
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteBrand(@PathVariable Integer id) {
        int result = brandService.deleteBrand(id);
        if (result > 0) {
            return ResponseEntity.ok("品牌删除成功");
        } else {
            return ResponseEntity.badRequest().body("品牌删除失败");
        }
    }
}
